/*
 * Copyright 2019-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.core.type.classreading;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.SpringAsmInfo;
import org.springframework.asm.Type;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.lang.Nullable;

/**
 * {@link MethodVisitor} implementation used by {@link DefaultClassDescriptorVisitor}
 * @author Brian Clozel
 */
class DefaultMethodDescriptorVisitor extends MethodVisitor {

	private static final String STATIC_METHOD_NAME = "<clinit>";

	private static final String CONSTRUCTOR_METHOD_NAME = "<init>";

	private final TypeSystem typeSystem;

	private final String declaringClassName;

	private final int access;

	private final String name;

	private final String descriptor;

	private List<MergedAnnotation<?>> annotations = new ArrayList<>();

	@Nullable
	private Source source;

	private final Consumer<MethodDescriptor> consumer;


	DefaultMethodDescriptorVisitor(TypeSystem typeSystem, String declaringClassName, int access, String name,
			String descriptor, Consumer<MethodDescriptor> consumer) {
		super(SpringAsmInfo.ASM_VERSION);
		this.typeSystem = typeSystem;
		this.declaringClassName = declaringClassName;
		this.access = access;
		this.name = CONSTRUCTOR_METHOD_NAME.equals(name) ? TypeName.from(declaringClassName).getConstructorName() : name;
		this.descriptor = descriptor;
		this.consumer = consumer;
	}

	@Override
	public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
		return MergedAnnotationReadingVisitor.get(this.typeSystem.getResourceLoader().getClassLoader(), this::getSource,
				descriptor, visible, this.annotations::add);
	}

	@Override
	public void visitEnd() {
		if (!STATIC_METHOD_NAME.equals(this.name)) {
			this.consumer.accept(this.getDescriptor());
		}
	}

	DefaultMethodDescriptor getDescriptor() {
		return new DefaultMethodDescriptor(this.typeSystem, this.name, this.access, this.declaringClassName, this.descriptor, MergedAnnotations.of(this.annotations));
	}

	private Object getSource() {
		DefaultMethodDescriptorVisitor.Source source = this.source;
		if (source == null) {
			source = new DefaultMethodDescriptorVisitor.Source(this.declaringClassName, this.name, this.descriptor);
			this.source = source;
		}
		return source;
	}


	/**
	 * {@link MergedAnnotation} source.
	 */
	static final class Source {

		private final String declaringClassName;

		private final String name;

		private final String descriptor;

		@Nullable
		private String toStringValue;

		Source(String declaringClassName, String name, String descriptor) {
			this.declaringClassName = declaringClassName;
			this.name = name;
			this.descriptor = descriptor;
		}

		@Override
		public int hashCode() {
			int result = 1;
			result = 31 * result + this.declaringClassName.hashCode();
			result = 31 * result + this.name.hashCode();
			result = 31 * result + this.descriptor.hashCode();
			return result;
		}

		@Override
		public boolean equals(@Nullable Object other) {
			if (this == other) {
				return true;
			}
			if (other == null || getClass() != other.getClass()) {
				return false;
			}
			DefaultMethodDescriptorVisitor.Source otherSource = (DefaultMethodDescriptorVisitor.Source) other;
			return (this.declaringClassName.equals(otherSource.declaringClassName) &&
					this.name.equals(otherSource.name) && this.descriptor.equals(otherSource.descriptor));
		}

		@Override
		public String toString() {
			String value = this.toStringValue;
			if (value == null) {
				StringBuilder builder = new StringBuilder();
				builder.append(this.declaringClassName);
				builder.append(".");
				builder.append(this.name);
				Type[] argumentTypes = Type.getArgumentTypes(this.descriptor);
				builder.append("(");
				for (Type type : argumentTypes) {
					builder.append(type.getClassName());
				}
				builder.append(")");
				value = builder.toString();
				this.toStringValue = value;
			}
			return value;
		}
	}
}
